PA 4: Military Spending

Tidy Data with dplyr and tidyr

# load packages
library(readxl) 
library(tidyverse)

Today you will be tidying messy data to explore the relationship between countries of the world and military spending.

Download starter .qmd file

Download data – gov_spending_per_capita.xlsx

Data Description

We will be using data from the Stockholm International Peace Research Institute (SIPRI). The SIPRI Military Expenditure Database is an open source data set containing time series on the military spending of countries from 1949–2019. The database is updated annually, which may include updates to data from previous years.

Military expenditure is presented in many ways:

  • in local currency and in US $ (both from 2018 and current);
  • in terms of financial years and calendar years;
  • as a share of GDP and per capita.

The availability of data varies considerably by country, but we note that data is available from at least the late 1950s for a majority of countries that were independent at the time. Estimates for regional military expenditure have been extended backwards depending on availability of data, but no estimates for total world military expenditure are available before 1988 due to the lack of data from the Soviet Union.

SIPRI military expenditure data is based on open sources only.

Data Import

First, you should notice that there are ten different sheets included in the dataset. We are interested in the sheet labeled “Share of Govt. spending”, which contains information about the share of all government spending that is allocated to the military.

Next, you’ll notice that there are notes about the data in the first six rows. Ugh! Also notice that the last six rows are footnotes about the data. Ugh!

Rather than copying this one sheet into a new Excel file and deleting the first and last few rows, let’s learn something new about the read_xlsx() function!

Data Import with read_xlsx()

The read_xlsx() function has several useful arguments:

  • sheet: specify the name of the sheet that you want to use. The name must be passed in as a string (in quotations)!
  • skip: specify the number of rows you want to skip before reading in the data.
  • n_max: specify the maximum number of rows of data to read in.

1. Modify the code below (potentially including the file path) to read the military expenditures data into your workspace.

military <- read_xlsx("gov_spending_per_capita.xlsx", 
                      sheet = , 
                      skip  = , 
                      n_max = )
Warning

If you have the Excel file open on your computer while trying to import the data, you may get an error. If you do, close the Excel file and try running your code again.

Data Cleaning

In addition to NAs, missing values were coded in two other ways.

2. Find these two methods and write code to replace these values with NAs. Save this dataset as a new object named military_clean.

Tip

The information in the top 6 rows of the excel sheet will help you answer this question.

Helpful functions: mutate(), across() – you will need two of these, na_if()

Note: When referring to one of the year variable names, you must put tick marks (above the tab key) around the name. Starting the name of a variable with a number is not commonly read as a variable name. E.g., to read the 1988 column through the 2018 column, use `1988`:`2019`.

# Code for completing Q2

Because characters were used to indicate missing values, all of the columns 1988 through 2019 were read in as characters.

3. Change these columns to a numeric data type. Save these changes into an updated version of military_clean.

# Code for Q3

If you give the Country column a look, you’ll see there are names of continents and regions included. These names are only included to make it simpler to find countries, as they contain no data.

Luckily for us, these region names were also stored in the “Regional totals” sheet. We can use the Region column of this dataset to filter out the names we don’t want.

Run the code below to read in the “Regional totals” dat, making any necessary modifications to the file path.

cont_region <- read_xlsx("gov_spending_per_capita.xlsx", 
                      sheet = "Regional totals", 
                      skip = 14) |> 
  filter(Region != "World total (including Iraq)", 
         Region != "World total (excluding Iraq)")

A clever way to filter out observations you don’t want is with a join. A tool tailored just for this scenario is the anti_join() function. This function will return all of the rows of one dataset without a match in another dataset.

4. Use the anti_join() function to filter out the Country values we don’t want in the military_clean data. The by argument needs to be filled with the name(s) of the variables that the two datasets should be joined with.

Tip

Join by different variables in dataX and dataY: join_by(a == b) will match dataX$a to dataY$b.

# Code for Q4
Canvas Q1

5. What four regions were NOT removed from the military_clean data set?

# Code for Q5
Tip

To answer this question, think about what uniquely separates the rows for the regions from the rows for the countries.

Useful functions: filter(), if_all(), is.na

Data Organization

We are interested in comparing the military expenditures of countries in Eastern Europe. Our desired plot looks something like this:

Desired plot: Countries from Central Asia used for demonstration – your plot will have different countries and spending values.
Warning

Unfortunately, if we want a point representing the spending for every country and year, we need every year to be a single column!

To tidy a dataset like this, we need to pivot the columns of years from wide format to long format. To do this process we need three arguments:

  • cols: The set of columns that represent values, not variables. In these data, those are all the columns from 1988 to 2019.

  • names_to: The name of the variable that should be created to move these columns into. In these data, this could be "Year".

  • values_to: The name of the variable that should be created to move these column’s values into. In these data, this could be labeled "Spending".

These form the three required arguments for the pivot_longer() function.

6. Pivot the cleaned up military data set to a “longer” orientation. Save this new “long” version as a new variable called military_long.

# Code for Q6
Caution

Do not overwrite your cleaned up dataset!

7. Notice that when you pivoted the data, the Year variable is a character data type. Convert this to numeric.

summary(military_long)
Error in eval(expr, envir, enclos): object 'military_long' not found
# Code for Q7.
# Make sure to save this change to military_long.

Data Visualization

Now that we’ve transformed the data, let’s create a plot to explore military spending across Eastern European countries.

8. Create side-by-side boxplots to explore the military spending between Eastern European countries.

Tip

Make sure you change the plot title and axis labels to accurately represent the plot.

You might also want to change the x-axis limits and the color of your plots.

Place the Year variable on an axis that makes it easier to read the labels!

# I have provided a list of Eastern European countries (in these data) for you to use.
eastern_europe <- c("Armenia", "Azerbaijan", "Belarus", 
                    "Georgia", "Moldova", "Russia", "Ukraine")

# Code for Q8.
Canvas Q2 + Q3

9. Looking at the plot you created above, which Eastern European country had the second highest median military expenditure?.

10. Looking at the plot you created above, which Eastern European country had the largest variability in military expenditures over time?